package com.netsdk.utils;


import com.alibaba.fastjson.JSONObject;
import com.netsdk.common.Base64;
import com.netsdk.config.LoginMes;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.sun.jna.Pointer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class DingYue {
    public static NetSDKLib netSdk = NetSDKLib.NETSDK_INSTANCE;
    public static NetSDKLib netConfig = NetSDKLib.CONFIG_INSTANCE;
    static int count=0;
    static Map<NetSDKLib.LLong,String> mapDingYueLogin = new HashMap<NetSDKLib.LLong,String>();
    //订阅回调类
    public static class DingYueCB implements NetSDKLib.fAnalyzerDataCallBack{
        public DingYueCB(){
        }
        private static DingYueCB dingYueCB = null;
        private static DingYueCB getInstance(){
            if(dingYueCB==null){
                dingYueCB = new DingYueCB();
            }
            return dingYueCB;
        }

        //订阅回调，获取进出记录信息
        @Override
        public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) {
            System.out.println(lAnalyzerHandle);
            File path = new File("./AccessPicture/");
            if (!path.exists()) {
                path.mkdir();
            }
            switch (dwAlarmType){
                case NetSDKLib.EVENT_IVS_ACCESS_CTL:{
                    NetSDKLib.DEV_EVENT_ACCESS_CTL_INFO msg = new NetSDKLib.DEV_EVENT_ACCESS_CTL_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);
                    //用户id
                    String userId = new String(msg.szUserID).trim();
                    //开门方式 2刷卡 16刷脸
                    int eventType = msg.emOpenMethod;
                    //1进2出
                    int inOut = msg.emEventType;
                    //用户类型
                    int userType = msg.emUserType;
                    //设备序列号
                    String snNumber = mapDingYueLogin.get(lAnalyzerHandle);
                    //获取base64编码器
                    Base64.Encoder encoder = Base64.getEncoder();
                    String base64ImgData = "";
                    for (int i=0;i<msg.nImageInfoCount;i++){
                        if(msg.stuImageInfo[i].emType==2){
                            byte[] faceBuf = pBuffer.getByteArray(
                                    msg.stuImageInfo[i].nOffSet,
                                    msg.stuImageInfo[i].nLength);
                            base64ImgData = encoder.encodeToString(faceBuf);
                            break;
                        }
                    }
                    JSONObject json = new JSONObject();
                    json.put("userId",userId);
                    json.put("eventType",eventType);
                    json.put("inOut",inOut);
                    json.put("snNumber",snNumber);
                    json.put("base64ImgData",base64ImgData);
                    String url = "http://localhost:8099/cj/tool/cyDaHuaCallback/inOutMes";
                    RestTemplate restTemplate = new RestTemplate();
                    MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(type);
                    HttpEntity<JSONObject> request = new HttpEntity<>(json,headers);
                    String res = restTemplate.postForObject(url,request,String.class);
                    System.out.println(res);
                    break;
                }
                default:
                    System.out.println("other--");
                    break;
            }
            return 0;
        }
    }

    static int bNeedPicture = 1; // 是否需要图片
    static int ChannelId = 0; // -1代表全通道 fAnalyzerDataCallBack
    public static void startDingYue(){
        for(Map.Entry<String, NetSDKLib.LLong> entry:LoginMes.loginMap.entrySet()){
            //开启订阅
            NetSDKLib.LLong m_hAttachHandle = netSdk.CLIENT_RealLoadPictureEx(entry.getValue(), ChannelId,
                    NetSDKLib.EVENT_IVS_ALL, bNeedPicture, DingYueCB.getInstance(), null, null);
            if(m_hAttachHandle.longValue()==0){
                System.out.println("订阅开启失败");
            }else{
                System.out.println("订阅开启成功");
                System.out.println(m_hAttachHandle.longValue());
                mapDingYueLogin.put(m_hAttachHandle,entry.getKey());
            }
        }
    }

}
